//
//  AFNetTool.m
//  SuperNews
//
//  Created by qiang on 16/1/8.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "AFNetTool.h"
@implementation AFNetTool

+ (void)getNetWithURL:(NSString *)url
                 body:(id)body
             headFile:(NSDictionary *)headFile
        responseStyle:(YZQResponseStyle)responseStyle
              success:(void (^)(NSURLSessionDataTask *, id))success
              failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
    // 1.创建网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.请求头的添加
    if (headFile) {
        for (NSString *key in headFile.allKeys) {
            [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
        }
    }
    
    // 3.返回数据的类型
    switch (responseStyle) {
        case YZQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case YZQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case YZQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    // 4.设置响应类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain", @"application/javascript", @"application/x-javascript", @"image/jpeg", nil]];
    // 5.UTF8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    // 6.发送请求
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(task, responseObject);
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[filePath stringByAppendingPathComponent:path]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (error) {
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[filePath stringByAppendingPathComponent:path]];
            if (responseObject) {
                success(task, responseObject);
            } else {
                failure(task, error);
            }
        }
    }];
}

+ (void)postNetWithURL:(NSString *)url
                  body:(id)body
             bodyStyle:(YZQRequesrtStyle)requestStyle
              headFile:(NSDictionary *)headFile
         responseStyle:(YZQResponseStyle)responseStyle
               success:(void (^)(NSURLSessionDataTask *, id))success
               failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
    // 1.创建网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.body的类型
    switch (requestStyle) {
        case YZQRequesrtJOSN:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case YZQRequesrtString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    // 3.请求头的添加
    if (headFile) {
        for (NSString *key in headFile.allKeys) {
            [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
        }
    }
    
    // 4.返回数据的类型
    switch (responseStyle) {
        case YZQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case YZQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case YZQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            
        default:
            break;
    }
    
    // 5.设置响应类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain", @"application/javascript", @"application/x-javascript", @"image/jpeg", nil]];
    // 6.UTF8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    // 7.发送请求
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (responseObject) {
            success(task, responseObject);
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[filePath stringByAppendingPathComponent:path]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (error) {
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[filePath stringByAppendingPathComponent:path]];
            if (responseObject) {
                success(task, responseObject);
            } else {
                failure(task, error);
            }
        }
    }];
    
    
}

@end
